package com.wc.test.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 过滤器
 * @author ysh
 */
public class CharacterEncodingFilter implements Filter {

	
	//当服务器启动就以及初始化了，随时等待过滤对象出现
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("CharacterEncodingFilter初始化");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		//过滤所有代码并对它的字符集编码设置为utf-8
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("CharacterEncodingFilter执行前....");
        chain.doFilter(request,response); //让我们的请求继续走，如果不写，程序到这里就被拦截停止！
        System.out.println("CharacterEncodingFilter执行后....");
	}

	//当关闭服务器的时候销毁过滤器
	public void destroy() {
		// TODO Auto-generated method stub
		 System.out.println("CharacterEncodingFilter销毁");
	}

}
